const os = require('os');
module.exports = {
  createGuid: function () {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
      let r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
      return v.toString(16);
    });
  },

  dateToString: function(now) {
    let year = now.getFullYear();
    let month = (now.getMonth() + 1).toString();
    let day = (now.getDate()).toString();

    if (month.length == 1) month = "0" + month;
    if (day.length == 1) day = "0" + day;

    let dateTime = `${year}-${month}-${day}`;
    return dateTime;
  },

  // 获取客户端IP
  get_client_ip : function () {
    var interfaces = os.networkInterfaces();
    for (var devName in interfaces) {
        var iface = interfaces[devName];
        for (var i = 0; i < iface.length; i++) {
            var alias = iface[i];
            if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
                return alias.address;
            }
        }
    }
  },

  // 获取当前用户Guid
  getCurrentUserGuid: function (req) {
    return req.session ? req.session.user.PK_UserGuid : null;
  },
};